<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 220px;height: 200px;background-color: red;
            position: absolute;top: 0;left: 0;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        // 1.获取鼠标在可视区的位置
        // event.clientX 横坐标
        // event.clientY 横坐标
        var box=document.querySelector('.box');
        document.onmousemove=function(e){
            e= e || event;
            // console.log(e);
            // 将鼠标不断变化的位置给到box
            box.style.left=e.clientX-box.offsetWidth/2+"px"
            box.style.top=e.clientY-box.offsetHeight/2+"px"
        }
    </script>
</body>
</html>